from manim import *


class FollowingGraphCamera(MovingCameraScene):
    def construct(self):
        ax = Axes(
            x_range=[-PI, 3 * PI, PI / 2],
            y_range=[-2, 8, 1],
        )
        graph = ax.plot(lambda x: np.sin(x), color=BLUE, x_range=[0, 3 * PI])
        self.add(ax, graph)

        dot_m = Dot(ax.i2gp(graph.t_min, graph), color=ORANGE)
        dot1 = Dot(ax.i2gp(graph.t_min, graph))
        dot2 = Dot(ax.i2gp(graph.t_max, graph), color=GREEN)
        self.add(dot1, dot2)

        # 相机移动
        self.camera.frame.save_state()
        self.play(self.camera.frame.animate.scale(0.5).move_to(dot1), run_time=2)

        def update_curve(mob):
            mob.move_to(dot_m.get_center())

        self.camera.frame.add_updater(update_curve)
        self.play(MoveAlongPath(dot_m, graph, rate_func=linear), run_time=5)
        self.camera.frame.remove_updater(update_curve)
        self.play(Restore(self.camera.frame), run_time=2)
